package com.yc.shopping.ui;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wb.swt.SWTResourceManager;

import com.yc.shopping.dao.AdminDao;
import com.yc.shopping.util.ShoppingUtil;

import java.sql.SQLException;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class AdminLoginUI {

	protected Shell shell;
	private Text text_aname;
	private Text text_pwd;
	
	AdminDao dao = new AdminDao();

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			AdminLoginUI window = new AdminLoginUI();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		//设置小图标
		
		shell.setImage(SWTResourceManager.getImage(AdminLoginUI.class, "/images/yclogo.png"));
		shell.setSize(526, 504);
		shell.setText("源辰收银系统后台登录");
		//窗口居中
		ShoppingUtil.centerWindows(shell);
		
		Label label = new Label(shell, SWT.NONE);
		label.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		label.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		label.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));
		label.setBounds(73, 62, 263, 36);
		label.setText("管理员登录后台界面");
		
		Label label_1 = new Label(shell, SWT.NONE);
		label_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		label_1.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		label_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		label_1.setBounds(36, 142, 90, 36);
		label_1.setText("用户名:");
		
		Label label_1_1 = new Label(shell, SWT.NONE);
		label_1_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		label_1_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		label_1_1.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		label_1_1.setText("密   码:");
		label_1_1.setBounds(36, 213, 90, 31);
		
		text_aname = new Text(shell, SWT.BORDER);
		text_aname.setBounds(132, 142, 221, 31);
		
		text_pwd = new Text(shell, SWT.BORDER | SWT.PASSWORD);
		text_pwd.setBounds(132, 213, 221, 31);
		
		Button btn_login = new Button(shell, SWT.NONE);
		
		btn_login.setBounds(80, 304, 114, 34);
		btn_login.setText("登录");
		
		Button btn_exit = new Button(shell, SWT.NONE);
		
		btn_exit.setBounds(288, 304, 114, 34);
		btn_exit.setText("退出");
		
		
		
		
		
		
		
		
		
		//管理员登录界面
		btn_login.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				//从界面获取用户名和密码
				String aname = text_aname.getText().trim();
				String pwd = text_pwd.getText().trim();
				
				//调用AdminDao中的login()
				try {
					Map<String,Object> map = dao.login(aname,pwd);
					//判断
					if(map == null) {
						//登录失败
						//弹框提示
//						MessageBox mb = new MessageBox(shell,SWT.NONE);
//						mb.setText("消息提示");
//						mb.setMessage("登录失败");
//						mb.open();
						ShoppingUtil.showMessage(shell, "错误提示", "登录失败");
						return;
					}
					 
					//登录成功 窗口跳转 进入主界面
					//收银员不能进入后台界面
					String type = map.get("ATYPE").toString();
					if("收银员".equals(type)) {
						ShoppingUtil.showMessage(shell, "错误提示", "权限不足");
						return;
					}
					//存储登录管理员的信息
					ShoppingUtil.adminLogin = map;
					
					
					//界面跳转
					AdminMainUI ui = new AdminMainUI();
					shell.dispose();//将当前窗口关闭
					ui.open();//打开新窗口
					
					
					
				} catch (SQLException e1) {
					ShoppingUtil.showMessage(shell, "异常信息", "登录错误" + e1.getMessage());
					e1.printStackTrace();
				}
				
				
			}
		});
		
		//退出系统
		btn_exit.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			}
		});

	}

}
